<?php

namespace App\Http\Controllers;

use App\Models\Member;
use App\Models\Tempphone;
use Illuminate\Http\Request;

class MemberController extends Controller
{
    public function loginPage(Request $request){
        if($request->cookie('authCookie')){
            return view('welcome');
        }else{
            return view('login');
        }

    }

    public function registerPage(){
        return view('register');
    }

    public function registerDao(Request $request){
        $request->validate([
            //使用嵌套参数的方式
            'Member.username'=>'required',
            'Member.password'=>'required',
            'confirmpwd'=>'required|same:Member.password',

            //自定义手机号码验证规则
            'Member.phone'=>'required|mobile',
            'vififycode'=>'required'
        ],[
            'required'=>':attribute 为必填项',
            'same'=>':attribute 不一致',
            'mobile'=>':attribute 格式不正确',
        ],[
            'Member.username'=>'用户名',
            'Member.password'=>'密码',
            'confirmpwd'=>'确认密码',
            'Member.phone'=>'手机号码',
            'vififycode'=>'短信验证码'
        ]);
        //使用嵌套参数的方式，返回的data是一个数组
        $data = $request->input('Member');

        $md5pwd = md5('yzm'.$data['password']);
        $data['password'] = $md5pwd;

        $phone = $data['phone'];
        $vififycode = $request->input('vififycode');

        //获取最新第一条短信验证码
        $tempphone = Tempphone::where('phone',$phone)
            ->orderBy('id','desc')
            ->first();

        if($tempphone->code == $vififycode){//如果数据库中的短信验证码等于用户输入的
            if(date('Y-m-d H-i-s',time())<$tempphone->deadline){
                Member::create($data);
                exit("<script>
                    alert('注册成功！')
                    location.href='loginPage'
                </script>");
            }else{
                exit("<script>
                    alert('短信验证码已经过期！')
                    location.href='registerPage'
                </script>");
            }
        }else{
            exit("<script>
                    alert('短信验证码错误！')
                    location.href='registerPage'
                </script>");
        }
    }

    public function loginDao(Request $request){
        //获取用户输入的表单信息
        $username = $request->input('username');
        $md5pwd = md5('yzm'.$request->input('password'));
        $capcode = $request->input('capcode');
        $autoLogin = $request->input('autoLogin');

        //从session获取图形验证码，与用户输入的图形验证码进行比对
        $capcode_img = $request->session()->get('captchaSession');

        //表单验证

        $result = Member::where([
            ['username','=',$username],
            ['password','=',$md5pwd]
        ])->first();

        if($capcode == $capcode_img){//验证图形验证码是否正确
            if($result){
                if($autoLogin==1){//选择了自动登录
                    //设置自动登陆的凭证
                    $salt = 'autologin';
                    $auth = md5($username.$md5pwd.$salt);

                    //设置cookie，用于存储用户自动登录的凭证，有效期为7天
                    $authCookie = \Cookie('authCookie',$auth,10080);
                    //设置cookie，用于存储应户名
                    $usernameCookie = \Cookie('usernameCookie',$username,10080);

                    return \response()->redirectToRoute('home')
                        ->cookie($authCookie)
                        ->cookie($usernameCookie);
                }else{
                    //设置临时登录的cookie
                    $usernameCookie = \Cookie('usernameCookie',$username);

                    return \response()->redirectToRoute('home')
                        ->cookie($usernameCookie);
                }
            }else{
                exit("<script>
                        alert('用户名或密码错误！');
                        location.href='loginPage';
                    </script>");
            }
        }else{
            exit("<script>
                        alert('图形验证码错误！');
                        location.href='loginPage';
                    </script>");
        }

    }

}
